package com.hkyp.ss.course.provider.impl;

import com.hkyp.ss.course.apis.DictionaryService;
import com.hkyp.ss.course.apis.dto.response.DictionaryResponseDTO;
import com.hkyp.ss.course.apis.enumu.EnableEnum;
import com.hkyp.ss.course.apis.utils.BeanMapper;
import com.hkyp.ss.course.provider.model.Dictionary;
import com.hkyp.ss.course.provider.repository.DicRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Service(version = "1.0")
@Transactional(rollbackFor = Exception.class)
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    DicRepository dicRepository;


    /**
     * 获取指定类型的字典
     *
     */
    public List<DictionaryResponseDTO> getDictionaryByType(Integer type){

        List<Dictionary> dictionaries = dicRepository.findAllByDicTypeAndStatus(type, EnableEnum.ENABLE.getCode());
        if(CollectionUtils.isEmpty(dictionaries)){
            return null;
        }
        List<DictionaryResponseDTO> dictionaryResponseDTOS = BeanMapper.mapList(dictionaries, DictionaryResponseDTO.class);

        return dictionaryResponseDTOS;
    }



}
